package edu.fzu.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.fzu.commonutils.ResponseResult;
import edu.fzu.eduservice.entity.EduTeacher;
import edu.fzu.eduservice.entity.vo.TeacherQuery;
import edu.fzu.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author JohnRambo
 * @since 2021-04-04
 */
@Api(description="讲师管理")
@RestController
@CrossOrigin
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    // 1.查询讲师列表中的所有数据
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public ResponseResult findAll(){
        List<EduTeacher> list = teacherService.list(null);
        return ResponseResult.ok().add("list", list);
    }

    // 2.逻辑删除讲师信息方法
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public ResponseResult removeTeacher(@PathVariable String id){
        boolean isSuccess = teacherService.removeById(id);
        return ResponseResult.ok().success(isSuccess);
    }

    /**
     * 3.分页查询讲师信息
     * @param current 当前页
     * @param limit 每页记录数
     * @return
     */
    @GetMapping("pageTeacher/{current}/{limit}")
    public ResponseResult pageListTeacher(@PathVariable long current,
                                          @PathVariable long limit){
        // 创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        teacherService.page(pageTeacher, null);

        // 总记录数
        long total = pageTeacher.getTotal();
        // 查询的结果
        List<EduTeacher> teacherList = pageTeacher.getRecords();
        return ResponseResult.ok().add("total", total).add("rows", teacherList);
    }

    // 4.条件查询待分页
    @PostMapping("pageTeacherByCondition/{current}/{limit}")
    public ResponseResult pageTeacherByCondition(@PathVariable long current, @PathVariable long limit,
                                                 @RequestBody(required = false) TeacherQuery teacherQuery){
        // 创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        // 构建查询条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 多条件组合(动态sql)
        String teacherName = teacherQuery.getName();
        Integer lever = teacherQuery.getLevel();
        String beginTime = teacherQuery.getBegin();
        String endTime = teacherQuery.getEnd();
        // 判断条件是否存在
        if (!StringUtils.isEmpty(teacherName)){
            wrapper.like("name", teacherName);
        }
        if (!StringUtils.isEmpty(lever)){
            wrapper.eq("level", lever);
        }
        if (!StringUtils.isEmpty(beginTime)){
            wrapper.ge("gmt_create", beginTime);
        }
        if (!StringUtils.isEmpty(endTime)){
            wrapper.le("gmt_create", endTime);
        }

        // 调用方法实现条件查询分页
        teacherService.page(pageTeacher, wrapper);

        // 总记录数
        long total = pageTeacher.getTotal();
        // 查询的结果
        List<EduTeacher> teacherList = pageTeacher.getRecords();
        return ResponseResult.ok().add("total", total).add("rows", teacherList);
    }

    // 5.添加讲师
    @PostMapping("addTeacher")
    public ResponseResult addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = teacherService.save(eduTeacher);
        return flag? ResponseResult.ok(): ResponseResult.error();
    }

    /**
     * 6.根据讲师id查询信息
     * @param id 讲师id
     * @return
     */
    @GetMapping("getTeacher/{id}")
    public ResponseResult getTeacherById(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return ResponseResult.ok().add("teacher", teacher);
    }

    // 修改讲师信息
    @PostMapping("updateTeacher")
    public ResponseResult updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = teacherService.updateById(eduTeacher);
        return flag? ResponseResult.ok(): ResponseResult.error();
    }
}

